package com.jeecms.wicool.manager.user.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.user.UserAccountDao;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.entity.user.UserAccount;
import com.jeecms.wicool.entity.user.UserInfo;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;
import com.jeecms.wicool.manager.user.UserAccountMng;
import com.jeecms.wicool.manager.user.UserInfoMng;

@Service
@Transactional
public class UserAccountMngImpl implements UserAccountMng {
	
	private UserAccountDao dao;
	@Autowired
	public void setDao(UserAccountDao dao) {
		this.dao = dao;
	}
	
	@Transactional(readOnly = true)
	public Pagination getPage(Integer shopId,String userName,String mobile,int pageNo, int pageSize,CmsUser user) {
		Pagination page = dao.getPage(shopId, userName, mobile, pageNo, pageSize,user);
		return page;
	}
	
	public Pagination getPage(String userName,String phoneNo,int pageNo, int pageSize,CmsUser user){
		return dao.getPage(userName,phoneNo, pageNo, pageSize, user);
	}
	
	@Override
	public UserAccount findById(Integer id) {
		UserAccount entity = dao.findById(id);
		return entity;
	}
	@Override
	public UserAccount update(UserAccount bean,Integer shopId,Integer userId) {
		
		ShopInfo shop = shopMng.findById(shopId);
		UserInfo user= userMng.findById(userId);
		bean.setShop(shop);
		bean.setUser(user);
		Updater<UserAccount> updater = new Updater<UserAccount>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}
	
	@Override
	public UserAccount save(UserAccount bean,Integer shopId,Integer userId) {
		
		ShopInfo shop = shopMng.findById(shopId);
		UserInfo user = userMng.findById(userId);
		bean.setShop(shop);
		bean.setUser(user);
		dao.save(bean);
		return bean;
	}

	@Override
	public UserAccount deleteById(Integer id) {
		UserAccount bean = dao.deleteById(id);
		return bean;
	}
	@Override
	public UserAccount[] deleteByIds(Integer[] ids) {
		UserAccount[] beans = new UserAccount[ids.length];
		for (int i = 0,len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}
	@Override
	public List<UserAccount> getListUserAccountByUserId(Integer userId) {
		return dao.getListUserAccountByUserId(userId);
	}
	
	@Override
	public List<UserAccount> getListUserAccountByVipLevel(Integer vipLevel) {
		return dao.getListUserAccountByVipLevel(vipLevel);
	}
	
	@Override
	public List<UserAccount> getListUserAccountByScore(Integer beginScore,
			Integer endScore) {
		
		return dao.getListUserAccountByScore(beginScore, endScore);
	}
	
	public List<UserAccount> getListUserAccountByIds(String ids){
		return dao.getListUserAccountByIds(ids);
	}
	
	public void batchUpdateVipLevel(Integer beginScore,Integer endScore,String vipLevel){
		dao.batchUpdateVipLevel(beginScore, endScore, vipLevel);
	}
	
	public void batchUpdateVipLevel(String ids,String vipLevel){
		dao.batchUpdateVipLevel(ids, vipLevel);
	}
	@Autowired
	private WiShopInfoMng shopMng;
	@Autowired
	private UserInfoMng userMng;
	
	
	
}